package com.example.doctor.mapper;

import org.apache.ibatis.jdbc.SQL;

import java.util.List;

public class BaseProvider {
    protected void filterFieldId(SQL sql, String fieldName, String[] ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        StringBuilder sbIds = new StringBuilder();
        for (int i = 0; i < ids.length; i++) {
            sbIds.append("'" + ids[i] + "'");
            if (i != ids.length - 1) {
                sbIds.append(",");
            }
        }
        sql.WHERE(fieldName + " IN (" + sbIds.toString() + ")");
    }

    protected void filterFieldIdList(SQL sql, String fieldName, List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            String no ="'noone'";
            sql.WHERE(fieldName + " IN (" + no + ")");;
            return;
        }
        StringBuilder sbIds = new StringBuilder();
        for (int i = 0; i < ids.size(); i++) {
            sbIds.append("'" + ids.get(i) + "'");
            if (i != ids.size() - 1) {
                sbIds.append(",");
            }
        }
        sql.WHERE(fieldName + " IN (" + sbIds.toString() + ")");
    }

    protected void filterFieldId(StringBuilder sql, String fieldName, String[] ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        StringBuilder sbIds = new StringBuilder();
        for (int i = 0; i < ids.length; i++) {
            sbIds.append("'" + ids[i] + "'");
            if (i != ids.length - 1) {
                sbIds.append(",");
            }
        }
        sql.append(" " + fieldName + " IN (" + sbIds.toString() + ") ");
    }


    protected void filterFieldNotId(SQL sql, String fieldName, String[] ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        StringBuilder sbIds = new StringBuilder();
        for (int i = 0; i < ids.length; i++) {
            sbIds.append("'" + ids[i] + "'");
            if (i != ids.length - 1) {
                sbIds.append(",");
            }
        }
        sql.WHERE(fieldName + " NOT IN (" + sbIds.toString() + ")");
    }

    protected void filterFieldNotIdList(SQL sql, String fieldName, List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            String no ="'noone'";
            sql.WHERE(fieldName + " IN (" + no + ")");;
            return;
        }
        StringBuilder sbIds = new StringBuilder();
        for (int i = 0; i < ids.size(); i++) {
            sbIds.append("'" + ids.get(i) + "'");
            if (i != ids.size() - 1) {
                sbIds.append(",");
            }
        }
        sql.WHERE(fieldName + " NOT IN (" + sbIds.toString() + ")");
    }


    protected void filterFieldNotId(StringBuilder sql, String fieldName, String[] ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        StringBuilder sbIds = new StringBuilder();
        for (int i = 0; i < ids.length; i++) {
            sbIds.append("'" + ids[i] + "'");
            if (i != ids.length - 1) {
                sbIds.append(",");
            }
        }
        sql.append("and " + fieldName + " NOT IN (" + sbIds.toString() + ") ");
    }

}
